<?php
/**
  * Voxbone Provisioning PHP Class
  * v.000000001
  * By Harper Reed
  * Harper@nata2.org
  * I am pretty awesome
  * 
  * I hate documenting my code. I cannot lie. But its pretty easy. 
  * I will make more documentation later. Basically you just do this:
  * 
  *
  * -----------------------------------------------------------
  *
  * Copyright 2006 Harper Reed
  * 
  * -----------------------------------------------------------
  *
  * LICENSE
  *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of the GNU General Public License (GPL)
  * as published by the Free Software Foundation; either version 2
  * of the License, or (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
  *
  * To read the license please visit http://www.gnu.org/copyleft/gpl.html
  *
  */

class voxboneAPI{

function voxboneAPI($login,$password){
	$this->soap_url = "http://sandbox.voxbone.com/ws/services/VoxService?wsdl";
	$this->login = $login;
	$this->password = $password;
	$this->client = new soapclient($this->soap_url, true );
}

function updateHash(){
	for ($i=0;$i<12;$i++)$rand .=  rand(1,9);
        $this->key = date("Y-m-d H:i:s").":".$rand;
        $this->hash = sha1( $this->password.$this->key );
}

function request($action,$params){
	$err = $this->client->getError();
	if ($err) {
		echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';
	}
	$result = $this->client->call($action, $params);
	if ($client->fault) {
		$this->fault = $this->client->fault;
		return -1;
	} else {
		// Check for errors
		$err = $this->client->getError();
		if ($err) {
			$this->err= $err;
			return 0;
		} else {
			return $result;
	   	}
	}
}

function soapClientDebug(){
	$debug .= '<h2>Key</h2><pre>' . htmlspecialchars($this->key, ENT_QUOTES) . '</pre>';
	$debug .= '<h2>Hash</h2><pre>' . htmlspecialchars($this->hash, ENT_QUOTES) . '</pre>';
	$debug .= '<h2>Request</h2><pre>' . htmlspecialchars($this->client->request, ENT_QUOTES) . '</pre>';
	$debug .= '<h2>Response</h2><pre>' . htmlspecialchars($this->client->response, ENT_QUOTES) . '</pre>';
	$debug .= '<h2>Fault</h2><pre>' . htmlspecialchars($this->fault, ENT_QUOTES) . '</pre>';
	$debug .= '<h2>Err</h2><pre>' . htmlspecialchars($this->err, ENT_QUOTES) . '</pre>';
	$debug .='<h2>Debug</h2><pre>' . htmlspecialchars($this->client->debug_str, ENT_QUOTES) . '</pre>';
	return $debug;
}

function payloadFactory($name,$params){
	$numParams = count($params);		
	if ($numParams>0){
		foreach($params as $pairname => $pairvalue){
			$xmlparams .= '<tns:'.$pairname.'>'.$pairvalue.'</tns:'.$pairname.'>' ;
		}
	}
	$xmlbody=
         '<tns:'.$name.' xmlns:tns="http://www.voxbone.com/VoxService"'
         .' xmlns:ns1="http://containers.services.webservices.voxbone">'
	.$this->getAuthentication()
	.$xmlparams
         .'</tns:'.$name.'>';
	return $xmlbody;
}

function getAuthentication(){
	$this->updateHash();
	$payload ='<tns:AuthenticationToken>'
                     .'<ns1:login>'.$this->login.'</ns1:login>'
                     .'<ns1:hash>'.$this->hash.'</ns1:hash>'
                     .'<ns1:key>'.$this->key.'</ns1:key>'
               .'</tns:AuthenticationToken>';
	return $payload;
}

function getCountriesList(){
	$action = "getCountriesList";
	$params = $this->payloadFactory($action,$params);
	$response = $this->request($action,$params);
	return $response;
}

function getAreaCodeForCountry($country_code_a2){
	$action = "getAreaCodeForCountry";
	$params = array('country_code_a2'=> $country_code_a2);
	$xml= $this->payloadFactory($action,$params);
	$response = $this->request($action,$xml);
	return $response;
}

function getCitiesInfo($country_code_a2, $areaCode ){
	$action = "getCitiesInfo";
	$params = array('country_code_a2'=> $country_code_a2, 'areaCode'=> $areaCode);
	$xml= $this->payloadFactory($action,$params);
        $response = $this->request($action,$xml);
	return $response;
}

function buyDIDs($countryPhoneCode,$areaCode,$areaCodeIdentifier,$quantity,$defaultURI){
	$action = "buyDIDs";
	$params = array('countryPhoneCode'=> $countryPhoneCode
			, 'areaCode'=> $areaCode
			, 'areaCodeIdentifier'=> $areaCodeIdentifier
			, 'quantity'=> $quantity
			, 'defaultURI'=> $defaultURI
			, 'purchaseInfo' => '');
        $xml= $this->payloadFactory($action,$params);
        $response = $this->request($action,$xml);
	return $response;
}

function buyDIDsByProductsID($productIdentifier, $quantity, $defaultURI){
	$action = "buyDIDsByProductsID";
	$params = array('productIdentifier'=> $productIdentifier
			, 'quantity'=> $quantity
			, 'defaultURI'=> $defaultURI
			, 'purchaseInfo' => '');
        $xml= $this->payloadFactory($action,$params);
        $response = $this->request($action,$xml);
	return $response;
}

function isPurchaseInfoValid($productIdentifier){
	$this->updateHash();
	$action = "isPurchaseInfoValid";
	$response = $this->request($action,$params);
	return $response;
}

function mapDIDToURI($DIDNumberFormatted, $URI){
	$action = "mapDIDToURI";
	$params = array('DIDNumberFormatted'=> $DIDNumberFormatted
                        , 'URI' => $URI);
        $xml= $this->payloadFactory($action,$params);
        $response = $this->request($action,$xml);
	return $response;
}

function getError(){
}

}

function purchaseInfo($first,$last,$salutation,$company,$street,$city,$zip,$country_code){
   $payload="<tns:purchaseInfo>
   <ns1:city>".$city."</ns1:city>
   <ns1:company>".$company."</ns1:company>
   <ns1:country_code_a2>".$country_code."</ns1:country_code_a2>
   <ns1:firstname>".$first."</ns1:firstname>
   <ns1:lastname>".$last."</ns1:lastname>
   <ns1:salutation>".$salutation."</ns1:salutation>
   <ns1:street>".$street."</ns1:street>
   <ns1:zip>".$zip."</ns1:zip>
   </tns:purchaseInfo>";
   $this->personalInfo = $payload;
}


?>
